

#ifndef __CONNECTION_H__
#define __CONNECTION_H__

#include "mempool.h"
#include "define.h"

#include<iostream>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>

#define  NONE_EVENT 0x00
#define  READ_EVENT 0x01
#define  WRITE_EVENT 0x02
#define  ALL_EVENT 0x04

/*@连接的结构体定义
 *@
 *
 */
struct connection{
	//这边后期用C++的仿函数来进行绑定试试看;
	size_t (*readEvent)(void *arg);
	size_t (*writeEvent)(void *arg);
	int  fd; 
	int accessCount; //被访问次数;
	struct timeval  last_accesstime;
	struct connection * next;
	char *readbuf;  //读缓冲;
	char *writebuf; //写缓冲;
	size_t bufsize;
};

//管理连接的;
class ConnectionSet{
	public:
		static size_t readDataFromConnNonblock(void * arg);
	public:
		int initConnection(int connectfd);
		ConnectionSet();
		~ConnectionSet();
		int init();  //初始化连接集合;
		int growHashTable(int fd);  //增加hash表;
		int deleteFreeConnection(int fd);//删除空闲连接;
		struct connection * getHashTable(int fd);
	private:
		struct connection **hashTable; //管理连接的hash表;
		struct connection *free_list; //回收空闲连接内存的
		int freeNodeListCount;
		MemPool mem;
		int hashnum;
};

/*@获取hash表;
 *
 *
 */
inline struct connection * ConnectionSet::getHashTable(int fd){
	return this->hashTable[fd];
}



#endif 

